home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4805 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. Path: news.MWCI.NET!usenet
  2. From: stuffle@pcii.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Missinterpreting *protected* access modifier?
  5. Date: 1 Feb 1996 02:11:42 GMT
  6. Organization: MidWest Communications, Inc.
  7. Message-ID: <4ep7gu$ao1@hihat.mwci.net>
  8. References: <DLrCGF.G1F@wn.planet.gen.nz>
  9. Reply-To: stuffle@pcii.net
  10. NNTP-Posting-Host: lan-pm1-16.pcii.net
  11. X-Newsreader: IBM NewsReader/2 v1.2.5
  12.  
  13. In <DLrCGF.G1F@wn.planet.gen.nz>, hugp@kp.planet.gen.nz (Peter Hug) writes:
  14. >Am I missinterpreting the meaning of the *protected* access modifier?
  15.  
  16. Sort of.
  17.  
  18. >The following code does not compile under VC++:
  19. >
  20. >     ============================================
  21. >     class A
  22. >     {
  23. >     protected:
  24. >       int i;
  25. >       int GetInt() { return i; };
  26. >     };
  27. >
  28. >     class B : public A
  29. >     {
  30. >     protected:
  31. >       int Foo(A * pA) { return pA->GetInt(); };
  32. >     };
  33. >     ============================================
  34. >
  35. >The error I'm getting is on the line declaring B::Foo(): error C2248:
  36. >'GetInt' : cannot access protected member declared in class 'A'
  37. >
  38. >My C++ bible knows the following about the protected keyword: "When
  39. >preceding a list of class members, the protected keyword specifies that
  40. >those members are accessible only from member functions and friends of
  41. >the class and its derived classes."
  42. >                  ^^^^^^^^^^^^^^^
  43.  
  44. But you are not trying to access it from the member of a derived class.  You are
  45. trying to access GetInt() from an object of class A.
  46.  
  47. >According to this definition, B should have access to A::GetInt().
  48. >
  49.  
  50. B does have access to it
  51.  
  52. B::Foo()
  53. {
  54.     return GetInt();
  55. }
  56.  
  57. for example would compile.  I guess what I am confused on is why you are 
  58. passing an pointer to an object of class A into Foo.  Remember, since B publicly
  59. inherits A, you are saying B "is a" A,  There is no reason to pass an object of
  60. type A into Foo, since:
  61.  
  62. B b;
  63.  
  64. b.Foo();
  65.  
  66. b, being on object of class B, is a A also.  IOW, you already know which object
  67. to call GetInt() for, namely b.
  68.  
  69. >Cheers
  70. >
  71. >
  72. >+-------------------------------------------------------------------+
  73. >+ Peter Hug                          mailto://hugp@kp.planet.gen.nz +
  74. >+ Maxi Solutions Ltd             http://kp.planet.gen.nz/users/hugp +
  75. >+ PO Box 1468                                                       +
  76. >+ Paraparaumu Beach                           Phone: +64 4 297-0463 +
  77. >+ New Zealand                                 Fax:   +64 4 297-0465 +
  78. >+-------------------------------------------------------------------+
  79. >
  80.  
  81. I really hope this helps.  e-mail me if you don't understand some of my babble,
  82. and I will try to be more clear.
  83.  
  84.  
  85.                                 \\\|///
  86.                                 | ~ ~ |
  87.                                (- @ @ -)
  88. //------------------------oOOo----(_)----oOOo------------------------
  89. //  Ken Sodemann  (Stuffle)             |   Get Warped!!!!
  90. //  Stuffle@PCII.NET                    |    
  91. //  http://users.mwci.net/~stuffle/     |   
  92. //-------------------------------------------------------------------
  93.  
  94.